// packageE/title/title.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();
Component({
  behaviors: [location],
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
  },
  options: {
    styleIsolation: 'shared',
  },
  // 私有数据，可用于模板渲染
  data: {
    location: {},
    clientWidth: "375",
    language:'',
    boxStyle:'',
    btnStyle:'',
    address: "",
    point: {
      lat: '',
      lng: ''
    },
    city: '',
    title: '',
    searchHotelData:{
      time:'',
      night:'',
      keyword:'',
      level:'',
      priceValue:0,
      startTime:'',
      endTime:''
    },
    current_index:0,
    star_value: 0,
    star_name:'',
    valueData:0,
    currentHour:0,
    star_arr: [{
        name: "二星及以下/经济型",
        value: 2
      },
      {
        name: "三星/舒适",
        value: 3
      },
      {
        name: "四星/高档",
        value: 4
      },
      {
        name: "五星/豪华",
        value: 5
      },
      {
        name: "六星",
        value: 6
      },
      {
        name: "七星",
        value: 7
      },
      {
        name: "八星",
        value: 8
      },
      {
        name: "不限",
        value: 0
      }
    ],
    showTimePopup:false,
    showLevelPopup:false,
    fixedPriceShow:false,
    formatter(day) {
      if (day.type === 'start') {
        day.bottomInfo = '入住';
      } else if (day.type === 'end') {
        day.bottomInfo = '离店';
      }
      return day;
    }
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    console.log(this.data.datas,'-------------dsfsfsdfdsdatas')
    this.getInitTime()
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
    let backgroundImage = this.data.datas.preview_color.image
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
      boxStyle:`padding-left:${this.data.datas.padding_left}px;padding-top:${this.data.datas.padding_top}px;padding-right:${this.data.datas.padding_right}px;padding-bottom:${this.data.datas.padding_bottom}px;background-color:${this.data.datas.preview_color.color};background-image:url(${backgroundImage})`,
      btnStyle:`color:${this.data.datas.button_color};background-color:${this.data.datas.button_bg_color};border-radius:${this.data.datas.button_border_radius}px;`
    });
    // this.showFixedPricePop()
    this._getLocation((mapdata, e) => {
      console.log(mapdata,'----------mapdata')
      this.setData({
        'point.lat': mapdata.lat,
        'point.lng': mapdata.lng,
        address: mapdata.address,
        title: mapdata.address,
        city: mapdata.address_component.city
      });
    });
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    keywordChange(e){
      this.setData({
        'searchHotelData.keyword': e.detail
      })
    },
    gotoSearch() {
      wx.navigateTo({
        url: '/packageC/hotel/HotelSearch/HotelSearch?start=' + this.data.start_time + '&end=' + this.data.end_time
      });
    },
    getInitTime() {
      let now = new Date();
      let next = new Date(now.getTime() + 24 * 60 * 60 * 1000);
      this.setData({
        'searchHotelData.startTime': `${now.getFullYear()}-${
          now.getMonth() + 1 < 10
            ? `0${now.getMonth() + 1}`
            : `${now.getMonth() + 1}`
        }-${now.getDate() < 10 ? `0${now.getDate()}` : `${now.getDate()}`}`
      });
      this.setData({
        'searchHotelData.endTime': `${next.getFullYear()}-${
          next.getMonth() + 1 < 10
            ? `0${next.getMonth() + 1}`
            : `${next.getMonth() + 1}`
        }-${next.getDate() < 10 ? `0${next.getDate()}` : `${next.getDate()}`}`
      });
      this.setData({
        'searchHotelData.time': `${this.data.searchHotelData.startTime} 至 ${this.data.searchHotelData.endTime}`,
        'searchHotelData.night':`共${this.nightNumber(now, next)}晚`
      })
    },
    routerGo(e) {
      const pages = getCurrentPages();
      const currentPage = pages[pages.length - 1];
      const url = `/${currentPage.route}?page_id=${this.data.page_id}`;
      wx.setStorageSync('diyurl', url);
      wx.navigateTo({
        url: '/packageC/o2o/o2oLocation/o2oLocation?tag=diy'
      });
    },
    starSubmit(e) {
      
      let tag = e.currentTarget.dataset.val;
      if (tag == "reset") {
        this.setData({
          current_index: 0,
          star_value: 2,
          'searchHotelData.priceValue': 0,
          'searchHotelData.level': '',
          valueData: 0
        });
      } else if (tag == "complete") {
        this.setData({
          showLevelPopup: false
        });
      }
    },
    goToFixedPrice: function(e) {
      const now = new Date()
      const currentHour = now.getHours()
      if (currentHour < 20) {
        app.tips('每晚20点-24点才可以订购酒店的一口价房间！')
        return
      }
      const { isclose } = e.currentTarget.dataset
      if (isclose) {
        this.closeFixedPricePop()
      }
      this.gotoSearch2(true)
    },
    closeFixedPricePop: function() {
      const now = new Date()
      wx.setStorageSync('fixedPriceDate', now.toLocaleDateString())
      this.setData({
        fixedPriceShow: false
      })
    },  
    gotoSearch2(isFixedPrice) {
      if (app._isTextEmpty(this.data.searchHotelData.startTime)) {
        wx.showToast({
          title: '请选择入住时间',
          icon: 'none',
          duration: 2000
        });
        return;
      }
  
      if (app._isTextEmpty(this.data.searchHotelData.endTime)) {
        wx.showToast({
          title: '请选择离店时间',
          icon: 'none',
          duration: 2000
        });
        return;
      }
  
      let fixedPrice = ''
      if (isFixedPrice == true) {
        fixedPrice = '&isFixedPrice=1'
      }
      wx.navigateTo({
        url: '/packageC/hotel/HotelSearch/HotelSearch?start=' + this.data.searchHotelData.startTime + '&end=' + this.data.searchHotelData.endTime +
          '&keyword='+ this.data.searchHotelData.keyword+'&price=' + this.data.searchHotelData.priceValue + '&star=' + this.data.star_value + fixedPrice
      });
    },
    showFixedPricePop: function() {
      const now = new Date()
      const currentHour = now.getHours()
      const nowStr = now.toLocaleDateString()
      const fixedPriceDate = wx.getStorageSync('fixedPriceDate')
      this.setData({ currentHour })
      if (currentHour >= 20 && (!fixedPriceDate || nowStr != fixedPriceDate)) {
        this.setData({
          fixedPriceShow: true
        })
      }
    },
    starChoose(e) {
      console.log(e,'-------------e')
      let index = e.currentTarget.dataset.index;
      let item = e.currentTarget.dataset.item;
      this.setData({
        current_index: index,
        star_value: item.value,
        star_name:item.name
      });
      let levelText = ''
      if(this.data.searchHotelData.priceValue !== 0){
        levelText = `${this.data.language['money']}${this.data.searchHotelData.priceValue}，${this.data.star_name}`
      } else {
        levelText = `${this.data.star_name}`
      }
      this.setData({
        'searchHotelData.level':levelText
      })
    },
    slidedrag(e) {
      let num = e.detail.value;
      let num_1 = num * 10;
      this.setData({
        'searchHotelData.priceValue': num_1
      });
      let levelText = ''
      if(this.data.star_name){
        if(this.data.searchHotelData.priceValue !== 0){
          levelText = `${this.data.language['money']}${this.data.searchHotelData.priceValue}，${this.data.star_name}`
        } else {
          levelText = `${this.data.star_name}`
        }
      } else {
        levelText = `${this.data.language['money']}${this.data.searchHotelData.priceValue}`
      }
      this.setData({
        'searchHotelData.level':levelText
      })
    },
    slideChange(e) {
      let num = e.detail;
      let num_1 = num * 10;
      this.setData({
        'searchHotelData.priceValue': num_1
      });
      let levelText = ''
      if(this.data.star_name){
        if(this.data.searchHotelData.priceValue !== 0){
          levelText = `${this.data.language['money']}${this.data.searchHotelData.priceValue}，${this.data.star_name}`
        } else {
          levelText = `${this.data.star_name}`
        }
      } else {
        levelText = `${this.data.language['money']}${this.data.searchHotelData.priceValue}`
      }
      this.setData({
        'searchHotelData.level':levelText
      })
    },
    selectDate(){
      this.setData({
        showTimePopup:true
      })
    },
    onTimeClose(){
      this.setData({
        showTimePopup:false
      })
    },
    formatDate(date) {
      date = new Date(date);
      let month = null
      let day = null
      if((date.getMonth() + 1)>=10){
        month = date.getMonth() + 1
      } else {
        month = '0'+(date.getMonth() + 1)
      }
      if(date.getDate()>=10){
        day = date.getDate()
      } else {
        day = '0'+date.getDate()
      }
      return `${date.getFullYear()}-${month}-${day}`;
    },
    nightNumber(startdate,enddate){
      const startTime = new Date(startdate).getTime();
      const endTime = new Date(enddate).getTime();
      return (endTime-startTime)/86400000
    },
    onTimeConfirm(event){
      const [start, end] = event.detail;
      console.log(start, end)
      this.setData({
        showTimePopup: false,
        'searchHotelData.startTime':this.formatDate(start),
        'searchHotelData.endTime':this.formatDate(end),
        'searchHotelData.time': `${this.formatDate(start)} 至 ${this.formatDate(end)}`,
        'searchHotelData.night':`共${this.nightNumber(start, end)}晚`
      });
    },
    onCloseLevelPopup(){
      this.setData({
        showLevelPopup:false
      })
    },
    selectLevel(){
      this.setData({
        showLevelPopup:true
      })
    }
  },
});
